local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local ActDefine = require("activity.Define")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local OperateActivityExcel = require("excel.OperateActivity")
local HorseLogic = require("horse.HorseLogic")
local PetLogic = require("pet.PetLogic")
local RoleDefine = require("role.RoleDefine")
local ObjHuman = require("core.ObjHuman")
local Equip = require("bag.Equip")
local OperateActFL = require("activity.OperateActFL")
local LimitBuy = require("activity.operate.LimitBuy")
local HtmlUtil = require("common.HtmlUtil")
local Log = require("common.Log")

function getConfig(id)
  if id == ActDefine.OPERATEACT_TYPE_SHENBING then
    return OperateActivityExcel.shenbing
  elseif id == ActDefine.OPERATEACT_TYPE_PET then
    return OperateActivityExcel.pet
  elseif id == ActDefine.OPERATEACT_TYPE_HORSE then
    return OperateActivityExcel.horse
  elseif id == ActDefine.OPERATEACT_TYPE_BAOWU then
    return OperateActivityExcel.baowu
  elseif id == ActDefine.OPERATEACT_TYPE_EQUIPSTAGE then
    return OperateActivityExcel.equipStage
  elseif id == ActDefine.OPERATEACT_TYPE_EQUIPQUALITY then
    return OperateActivityExcel.equipQuality
  elseif id == ActDefine.OPERATEACT_TYPE_EQUIPPOWER then
    return OperateActivityExcel.equipPower
  end
end

function getActName(id)
  if id == ActDefine.OPERATEACT_TYPE_SHENBING then
    return "神兵强化"
  elseif id == ActDefine.OPERATEACT_TYPE_PET then
    return "神将培养"
  elseif id == ActDefine.OPERATEACT_TYPE_HORSE then
    return "铁骑升级"
  elseif id == ActDefine.OPERATEACT_TYPE_BAOWU then
    return "宝物升级"
  elseif id == ActDefine.OPERATEACT_TYPE_EQUIPSTAGE then
    return "装备升阶"
  elseif id == ActDefine.OPERATEACT_TYPE_EQUIPQUALITY then
    return "装备提品"
  elseif id == ActDefine.OPERATEACT_TYPE_EQUIPPOWER then
    return "装备战力"
  end
end

function open(human, id, type)
  local sendMsg = Msg.gc.GC_OPEN_YUNYING_ACTIVITY
  local activityNum
  if type == 2 then
    activityNum = OperateActFL.onGetTomorrowActRuleID(id)
  else
    activityNum = OperateActFL.onGetCurActRuleID(id)    
  end
  local curActConfig = getConfig(id)
  local activity = curActConfig[activityNum]
  if not activity then
    return
  end
  sendMsg.info[0] = #activity.condition  
  local cache = {}
  for i=1, #activity.condition do
    sendMsg.info[i].reward[0] = #activity.reward[i]
    for j=1, #activity.reward[i] do
      Grid.create(cache, activity.reward[i][j][1], activity.reward[i][j][2])
      Grid.makeItem(cache, sendMsg.info[i].reward[j], 0, human)
    end    
    
    if activity.needCnt then
      sendMsg.info[i].args[0] = 2
      sendMsg.info[i].args[1] = activity.condition[i]
      sendMsg.info[i].args[2] = activity.needCnt[i]
    else
      sendMsg.info[i].args[0] = 1
      sendMsg.info[i].args[1] = activity.condition[i]
    end
    sendMsg.info[i].canGet = human.db.YYDaBiaoStatus[(id-5)*7 + i - 1]
    sendMsg.info[i].name = ""
  end
  sendMsg.myInfo[0] = 1
  sendMsg.myInfo[1] = 0--getMyInfo(human, id)
  sendMsg.activityId = id
  sendMsg.type = type or 1
  Msg.send(sendMsg, human.fd)
  LimitBuy.dabiaoOpen(human, id, sendMsg.type)
end

function getReward(human, id, index)
  if human.db.YYDaBiaoStatus[(id-5)*7 + index - 1] ~= ActDefine.REWARD_CAN_GET then
    return
  end
  local activityNum = OperateActFL.onGetCurActRuleID(id)
  local curActConfig = getConfig(id)
  local activity = curActConfig[activityNum]
  if not activity then
    return
  end
  if not BagLogic.checkCanAddItemList(human, activity.reward[index], #activity.reward[index]) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_BAG_NOT_ENOUGH)
  end
  
  for i=1, #activity.reward[index] do
    BagLogic.addItem(human, activity.reward[index][i][1], 1, activity.reward[index][i][2], "yunyingAcvity")
  end
  
  human.db.YYDaBiaoStatus[(id-5)*7 + index - 1] = ActDefine.REWARD_ALREADY_GET
  open(human, id)
  
  local str = string.format(Lang.YUNYING_DABIAO_BRO, HtmlUtil.fontName(human.name, human._id), getActName(id), human._id)
  Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
  Log.write(Log.LOGID_OSS_YUNYING_DABIAO, human._id, human.account, human.name,human.db.lv, id, index)
end

--检查是否可领取
function checkCanGet(human, id, nowInfo)  
  if not OperateActFL.onCheckActIsRunning(id) then
    return
  end  
  local activityNum = OperateActFL.onGetCurActRuleID(id)
  local curActConfig = getConfig(id)
  local activity = curActConfig[activityNum]
  if not activity then    
    return
  end
  local check = false
  if id == ActDefine.OPERATEACT_TYPE_SHENBING then
    for i=1, #activity.condition do   
      for j = 0, human.db.shenbingLen - 1  do
        if human.db.shenbing[j].star >= activity.needCnt[i] and human.db.shenbing[j].lv >= activity.condition[i] 
            and human.db.YYDaBiaoStatus[(id-5)*7 + i - 1] == ActDefine.REWARD_NOT_CAN_GET then
          human.db.YYDaBiaoStatus[(id-5)*7 + i - 1] = ActDefine.REWARD_CAN_GET
        end
      end
    end
    check = true
  elseif id == ActDefine.OPERATEACT_TYPE_PET then
  elseif id == ActDefine.OPERATEACT_TYPE_HORSE then 
    nowInfo = HorseLogic.getHorseLv(human)   
  elseif id == ActDefine.OPERATEACT_TYPE_BAOWU then
    local power = 0
    for i = 0, human.db.baowuLen - 1  do
      power = power + human.db.baowu[i].power
    end
    nowInfo = power
  elseif id == ActDefine.OPERATEACT_TYPE_EQUIPSTAGE then    
    for i=1, #activity.condition do    
      if Equip.getEquipLvCnt(human, activity.condition[i], activity.needCnt[i]) >= activity.needCnt[i] and human.db.YYDaBiaoStatus[(id-5)*7 + i - 1] == ActDefine.REWARD_NOT_CAN_GET then
        human.db.YYDaBiaoStatus[(id-5)*7 + i - 1] = ActDefine.REWARD_CAN_GET
      end
    end
    check = true
  elseif id == ActDefine.OPERATEACT_TYPE_EQUIPQUALITY then    
    for i=1, #activity.condition do    
      if Equip.getEquipQualityCnt(human, activity.condition[i], activity.needCnt[i]) >= activity.needCnt[i] and human.db.YYDaBiaoStatus[(id-5)*7 + i - 1] == ActDefine.REWARD_NOT_CAN_GET then
        human.db.YYDaBiaoStatus[(id-5)*7 + i - 1] = ActDefine.REWARD_CAN_GET
      end
    end
    check = true
  elseif id == ActDefine.OPERATEACT_TYPE_EQUIPPOWER then
    nowInfo = ObjHuman.getEquipTotalPower(human)
  end
  if not check then
    for i=1, #activity.condition do    
      if nowInfo >= activity.condition[i] and human.db.YYDaBiaoStatus[(id-5)*7 + i - 1] == ActDefine.REWARD_NOT_CAN_GET then
        human.db.YYDaBiaoStatus[(id-5)*7 + i - 1] = ActDefine.REWARD_CAN_GET
      end
    end
  end
  open(human, id, 1)
end
